package 并发编程15.线程池;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author idea
 * @data 2019/2/22
 */
public class newScheduleThreadPoolDemo {

    public static void main(String[] args) {
        //设定定长的线程池，支持定时以及周期性的任务执行
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);
        scheduledExecutorService.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("延迟3秒后返回");
            }
        }, 3, TimeUnit.SECONDS);

        //延迟一秒之后自动执行，时间间隔是三秒
        ScheduledExecutorService scheduledExecutorService1 = Executors.newScheduledThreadPool(5);
        scheduledExecutorService1.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + ":延迟三秒后返回");
            }
        }, 1, 3, TimeUnit.SECONDS);
    }
}
